/**
 * Copyright (c) 2017-2020 四川智慧云天 All rights reserved.
 *
 * https://www.sczhyt.cn
 *
 * 版权所有，侵权必究！
 */

package cn.sczhyt.modules.sys.service.impl;

import cn.sczhyt.common.utils.ChineseInital;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;
import cn.sczhyt.modules.sys.dao.SysDictDao;
import cn.sczhyt.modules.sys.entity.SysDictEntity;
import cn.sczhyt.modules.sys.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {
    @Autowired
    SysDictDao sysDictDao;

    private List list=new ArrayList<SysDictEntity>();
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        String type= (String) params.get("type");
        if(type!=null && !"".equals(type))
            wrapper.eq("type",type);
        String name=(String) params.get("name");
        if(name!=null && !"".equals(name))
            wrapper.like("name",name);
        //分类型查询
        IPage<SysDictEntity> page = this.page(
            new Query<SysDictEntity>().getPage(params),//分页查询
            wrapper.orderByAsc("order_num")
        );
        return new PageUtils(page);
    }

    @Override
    public List<SysDictEntity> findAll() {
        List<SysDictEntity> dicts=sysDictDao.findAll();
        return dicts;
    }

    @Override
    public List<SysDictEntity> findjc() {
        List<SysDictEntity> lists = sysDictDao.fandjc();
        list.clear();
        for (SysDictEntity sysDictEntity : lists) {
            if(sysDictEntity.getPId()==null) {
                list.add(sysDictEntity);
            }
        }
        if (list.size()>=1) {
            Object o = list.get(list.size() - 1);
            list.clear();
            list.add(o);
        }
        list.add(lists.get(lists.size()-1));
        return list;
    }

    @Override
    public void Save(SysDictEntity dict) {

        String code = dict.getCode();
        String firstLetter = ChineseInital.getAllFirstLetter(code);
        dict.setType(firstLetter);
        sysDictDao.insert(dict);
    }
}
